<html>
<head><meta charset="utf-8"><title>quick poll: `as` vs `transmute` · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/quick.20poll.3A.20.60as.60.20vs.20.60transmute.60.html">quick poll: `as` vs `transmute`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="154148007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/quick%20poll%3A%20%60as%60%20vs%20%60transmute%60/near/154148007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/quick.20poll.3A.20.60as.60.20vs.20.60transmute.60.html#154148007">(Jan 01 2019 at 23:54)</a>:</h4>
<p>Please vote which you prefer using some emoji! I didn't test either, but I hope the intent is clear.</p>



<a name="154148048"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/quick%20poll%3A%20%60as%60%20vs%20%60transmute%60/near/154148048" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/quick.20poll.3A.20.60as.60.20vs.20.60transmute.60.html#154148048">(Jan 01 2019 at 23:54)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="p">(</span><span class="n">v</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">Val</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="n">WrappedVal</span><span class="p">)</span><span class="w"></span>
</pre></div>



<a name="154148058"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/quick%20poll%3A%20%60as%60%20vs%20%60transmute%60/near/154148058" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/quick.20poll.3A.20.60as.60.20vs.20.60transmute.60.html#154148058">(Jan 01 2019 at 23:55)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="n">mem</span>::<span class="n">transmute</span>::<span class="o">&lt;&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">Val</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">WrappedVal</span><span class="o">&gt;</span><span class="p">(</span><span class="n">v</span><span class="p">)</span><span class="w"></span>
</pre></div>



<a name="154149516"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/quick%20poll%3A%20%60as%60%20vs%20%60transmute%60/near/154149516" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alexander Regueiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/quick.20poll.3A.20.60as.60.20vs.20.60transmute.60.html#154149516">(Jan 02 2019 at 00:44)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> Yeah, I'm chipping away at that one... should have a WIP PR up in a few days I think.</p>



<a name="154150341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/quick%20poll%3A%20%60as%60%20vs%20%60transmute%60/near/154150341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> snf <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/quick.20poll.3A.20.60as.60.20vs.20.60transmute.60.html#154150341">(Jan 02 2019 at 01:15)</a>:</h4>
<p>ByteOrder uses the first one though: <a href="https://github.com/BurntSushi/byteorder/blob/master/src/lib.rs#L744" target="_blank" title="https://github.com/BurntSushi/byteorder/blob/master/src/lib.rs#L744">https://github.com/BurntSushi/byteorder/blob/master/src/lib.rs#L744</a></p>



<a name="154151557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/quick%20poll%3A%20%60as%60%20vs%20%60transmute%60/near/154151557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/quick.20poll.3A.20.60as.60.20vs.20.60transmute.60.html#154151557">(Jan 02 2019 at 01:56)</a>:</h4>
<p>I have a little voice in the back of my head that says "avoid <code>transmute</code> as much as possible", but I also feel that someone (maybe <span class="user-mention" data-user-id="119009">@eddyb</span> ?) had a set of guidelines when <code>transmute</code> was "better".</p>



<a name="154169920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/quick%20poll%3A%20%60as%60%20vs%20%60transmute%60/near/154169920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/quick.20poll.3A.20.60as.60.20vs.20.60transmute.60.html#154169920">(Jan 02 2019 at 11:19)</a>:</h4>
<p><code>&amp;mut *(v as *mut _ as *mut T)</code> is preferred. One case where it catches an issue (and transmute won’t) is if you’re casting between fat pointers. </p>
<p>EDIT: oh wait... does it? <br>
EDIT2: yeah, it does.</p>
<div class="codehilite"><pre><span></span>error[E0606]: casting `*mut dyn std::fmt::Display` as `*mut dyn std::fmt::Debug` is invalid
</pre></div>



<a name="154169993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/quick%20poll%3A%20%60as%60%20vs%20%60transmute%60/near/154169993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/quick.20poll.3A.20.60as.60.20vs.20.60transmute.60.html#154169993">(Jan 02 2019 at 11:20)</a>:</h4>
<p>I was also going to say that you avoid accidentally increasing the lifetime, but it turns out that it happens with the casts as well <span class="emoji emoji-1f641" title="frown">:frown:</span> </p>
<div class="codehilite"><pre><span></span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="kt">u32</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nb">&#39;static</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="kt">u32</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="c1">// both work...</span>
<span class="w">    </span><span class="c1">//std::intrinsics::transmute::&lt;&amp;mut u32, &amp;mut u32&gt;(x)</span>
<span class="w">    </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="p">(</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">u32</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="154180752"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/quick%20poll%3A%20%60as%60%20vs%20%60transmute%60/near/154180752" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/quick.20poll.3A.20.60as.60.20vs.20.60transmute.60.html#154180752">(Jan 02 2019 at 15:26)</a>:</h4>
<p>OTOH <code>transmute</code> at least screams DANGER, whereas <code>as</code> of a raw ptr type looks awfully harmless</p>



<a name="154180777"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/quick%20poll%3A%20%60as%60%20vs%20%60transmute%60/near/154180777" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/quick.20poll.3A.20.60as.60.20vs.20.60transmute.60.html#154180777">(Jan 02 2019 at 15:27)</a>:</h4>
<p>as a reviewer I very strongly dislike these raw ptr casts, they are so easy to miss</p>



<a name="154183614"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/quick%20poll%3A%20%60as%60%20vs%20%60transmute%60/near/154183614" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/quick.20poll.3A.20.60as.60.20vs.20.60transmute.60.html#154183614">(Jan 02 2019 at 16:15)</a>:</h4>
<p>how about <code>unsafe fn almost_transmute&lt;T, U&gt;(x: &amp;mut T) -&gt; &amp;mut U { ... } </code>? Preserves the lifetime too!</p>



<a name="154183696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/quick%20poll%3A%20%60as%60%20vs%20%60transmute%60/near/154183696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/quick.20poll.3A.20.60as.60.20vs.20.60transmute.60.html#154183696">(Jan 02 2019 at 16:16)</a>:</h4>
<p>bikeshed: <code>transmute_almost</code> (since we have <code>transmute_copy</code>)</p>



<a name="154184022"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/quick%20poll%3A%20%60as%60%20vs%20%60transmute%60/near/154184022" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/quick.20poll.3A.20.60as.60.20vs.20.60transmute.60.html#154184022">(Jan 02 2019 at 16:20)</a>:</h4>
<p><code>transmute_me_maybe</code>.</p>



<a name="154185696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/quick%20poll%3A%20%60as%60%20vs%20%60transmute%60/near/154185696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/quick.20poll.3A.20.60as.60.20vs.20.60transmute.60.html#154185696">(Jan 02 2019 at 16:45)</a>:</h4>
<p><code>transmute_ref</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>